package cn.dnui_001zjs.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.dnui_001zjs.po.User;
import cn.dnui_001zjs.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;

	public UserController() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/admin/login.action", method = RequestMethod.POST)
	public String login(String username,String password, Model model, 
                                                          HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(username, password);
		if(user != null){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
			return "redirect:index.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "admin/login";
	}
	

	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/admin/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
	    session.invalidate();
	    // 重定向到登录页面的跳转方法
	    return "redirect:/admin/login.action";
	}
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/admin/login.action", method = RequestMethod.GET)
	public String toLogin() {
	    return "admin/login";
	}
	@RequestMapping("/admin/index.action")
	public String ulist(Model model){
		User user  = userService.getUser();
		model.addAttribute("user", user);
		return "admin/ulist";
	}

}
